package com.molichuxing.gateway.api.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.PaymentApiService;
import com.molichuxing.gateway.bff.payment.entity.response.AlipayLogVo;
import com.molichuxing.gateway.bff.payment.entity.response.WechatpayLogVo;
import com.molichuxing.gateway.bff.payment.services.EscrowLogBffService;
import com.molichuxing.gateway.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "支付查询相关接口")
@RestController
@RequestMapping(value = "/payment")
public class PaymentApiServiceImpl extends RootApiService implements PaymentApiService {
    private static final Logger logger = LoggerFactory.getLogger(PaymentApiServiceImpl.class);

    @Resource
    private TokenUtil tokenUtil;

    @Resource
    private EscrowLogBffService escrowLogBffService;

    @Override
    @ApiOperation(value = "获取微信支付日志列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "payNumber", value = "商户号订单（付款流水号）", required = false, dataType = "String"),
            @ApiImplicitParam(name = "startRequestTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "endRequestTime", value = "结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

    })
    @GetMapping(value = "/wechatpay/log")
    public ResponseResult<Paged<WechatpayLogVo>> getWechatpayLogList(
            @RequestParam Map<String, Object> params, @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        ResponseResult<Paged<WechatpayLogVo>> result = new ResponseResult<>();
        try {
            result.success(escrowLogBffService.getWechatpayLog(params, pageNum, pageSize));
        } catch (BizException e) {
            logger.error("[getWechatpayLogList]获取微信支付日志列表异常", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            // 记录异常，返回异常信息
            logger.error("[getWechatpayLogList]获取微信支付日志列表异常", e);
            result.error("获取微信支付日志列表异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "获取支付宝支付日志列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "payNumber", value = "商户号订单（付款流水号）", required = false, dataType = "String"),
            @ApiImplicitParam(name = "startRequestTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "endRequestTime", value = "结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

    })
    @GetMapping(value = "/alipay/log")
    public ResponseResult<Paged<AlipayLogVo>> getAlipayLogList(
            @RequestParam Map<String, Object> params, @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        ResponseResult<Paged<AlipayLogVo>> result = new ResponseResult<>();
        try {
            result.success(escrowLogBffService.getAlipayLog(params, pageNum, pageSize));
        } catch (BizException e) {
            logger.error("[getAlipayLogList]获取支付宝支付日志列表异常", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            // 记录异常，返回异常信息
            logger.error("[getAlipayLogList]获取支付宝支付日志列表异常", e);
            result.error("获取支付宝支付日志列表异常");
        }
        return result;
    }
}
